using System;
using System.Linq;
using Azure.Domain.Models;
using Azure.Infrastructure;
using Azure.Membership;

namespace Azure.Web.Stubs
{
    public class StubMembershipRepository : IMembershipRepository
    {
        private readonly IRepository<User> _repository;

        public StubMembershipRepository(IRepository<User> repository)
        {
            _repository = repository;
        }

        public User LoadByEmail(string email)
        {
            var users = from u in _repository.Table where u.Email == email select u;
            return users.First();
        }

        public User LoadByUsername(string username)
        {
            User result = null;
            var users = from u in _repository.Table where u.UserName == username select u;

            var enumerator = users.GetEnumerator();
            if (enumerator != null && enumerator.MoveNext())
            {
                result = enumerator.Current;
            }

            return result;
        }

        public void Create(User user)
        {
            _repository.Insert(user);
        }
    }
}